#include "config.h"

#define	YO	7
#define SELECTX	3
#define R 	2

U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/12, /* dc=*/4, /* reset=*/6);

typedef struct {
	unsigned char current;
	unsigned char up;
	unsigned char down;
	unsigned char enter;
	unsigned char exit;
	void (*current_operation)();
} key_table;

unsigned char func_index = 0;
boolean P = true;
boolean F = false;
char i_str[3];
unsigned int obj_x = 10;
unsigned int obj_y = 10;

void (*current_operation_index) ();

void page()
{
	strcpy(i_str, u8x8_u8toa(func_index, 2)); /* convert func_index to a string with two digits */
	u8g2.setFont(u8g2_font_4x6_tr);
	u8g2.drawStr(120, 6, i_str);
	u8g2.setFont(u8g2_font_6x10_tf);

	if (func_index > 0 && func_index < 7) {
		u8g2.drawStr(10, 10, "1 Draw Box");
		u8g2.drawStr(10, 20, "2 Draw Btn");
		u8g2.drawStr(10, 30, "3 Mutli");
		u8g2.drawStr(10, 40, "entry 4");
		u8g2.drawStr(10, 50, "entry 5");
		u8g2.drawStr(10, 60, "entry 6");
	}
}

void page_seleced()
{
	u8g2.clearBuffer();
	if (func_index >0 && func_index < 7) {
		page();
		u8g2.drawCircle(SELECTX, func_index*10-4, R, U8G2_DRAW_ALL);
	}
  	u8g2.sendBuffer();
}


void func()
{
	if(func_index == 7) {
		F = true;
		P = false;
		u8g2.clearBuffer();
		u8g2.drawBox(obj_x,obj_y, 10, 10);
		u8g2.sendBuffer();
	}
	if(func_index == 8) {
		F = true;
		P = false;
		u8g2.clearBuffer();
		u8g2.setFont(u8g2_font_helvR08_tr);
		u8g2.drawButtonUTF8(obj_x, obj_y, U8G2_BTN_INV|U8G2_BTN_BW2, 0,  2,  2, "Btn" );
		u8g2.sendBuffer();
	}
}

void welcome_page()
{
	u8g2.clearBuffer();
  	u8g2.drawStr(10, 10, "Welcome!");
	u8g2.drawStr(10, 20, "A for menu");
  	u8g2.sendBuffer();
}

key_table table[12] = 
{
	{0, 0, 0, 1, 0, (*welcome_page)},
	{1, 6, 2, 7, 0, (*page_seleced)},
	{2, 1, 3, 8, 0, (*page_seleced)},
	{3, 2, 4, 9, 1, (*page_seleced)},
	{4, 3, 5, 1, 0, (*page_seleced)},
	{5, 4, 6, 1, 0, (*page_seleced)},
	{6, 5, 1, 1, 0, (*page_seleced)},
	{7, 1, 1, 1, 1, (*func)},
	{8, 1, 1, 1, 1, (*func)},
};



void setup()
{
	pinMode(KEY_U, INPUT_PULLUP);	// up
	pinMode(KEY_D, INPUT_PULLUP);	// down 
	pinMode(KEY_L, INPUT_PULLUP);	// left
	pinMode(KEY_R, INPUT_PULLUP);	// right
	pinMode(KEY_A, INPUT_PULLUP); 	// A 
	pinMode(KEY_B, INPUT_PULLUP);	// B
	
	u8g2.begin();
	Serial.begin(9600);
	u8g2.setFont(u8g2_font_6x10_tf);
	u8g2.setCursor(0, 20);
	page();
}

void loop(void)
{
	if(KEY_U0 || KEY_D0 || KEY_A0 || KEY_B0 || KEY_L0 || KEY_R0){
		delay(10);
		if (P){
			if(KEY_U0) {
				func_index = table[func_index].up;
				while(!KEY_U1);
			}
			if(KEY_D0) {
				func_index = table[func_index].down;
				while(!KEY_D1);
			}
			if(KEY_A0) {
				func_index = table[func_index].enter;
				while(!KEY_A1);
			}
			if(KEY_B0) {
				func_index = table[func_index].enter;
				while(!KEY_B1);
			}
		}
		if(F) {
			if(KEY_U0) obj_y--;
			if(KEY_D0) obj_y++;
			if(KEY_L0) obj_x--;
			if(KEY_R0) obj_x++;
			if(KEY_B0) {
				delay(10);
				if(KEY_B0) {F=false; P=true; func_index=1; while(!KEY_B1);}
			}
		}
	}
	current_operation_index = table[func_index].current_operation;
	(*current_operation_index)();
}